﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Libs.Db;

namespace Libs.HR
{
    public class Family
    {
        public int FamilyID { get; set; }
        public int EmployeeID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string BirthDay { get; set; }
        public int TypeID { get; set; }
        public string Address { get; set; }
        public string Description { get; set; }
        public int ReturnValue { get; set; }

        public Family()
        {

        }

        ~Family()
        {

        }

        public virtual void Dispose()
        {

        }

        /// <summary>
        /// Lấy giá trị Family theo ID
        /// </summary>
        /// <param name="familyID"></param>
        /// <returns></returns>
        public Family Get(int familyID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetInstanceSP<Family>("sp_Family_Select",
                new SqlParameter("@FamilyID", familyID));
        }

        /// <summary>
        /// Lấy giá trị Family theo ID
        /// </summary>
        /// <returns></returns>
        public Family Get()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetInstanceSP<Family>("sp_Family_Select",
                new SqlParameter("@FamilyID", FamilyID));
        }

        /// <summary>
        /// Thêm mới Family
        /// </summary>
        public void Add()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[8];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@EmployeeID", EmployeeID);
            pars[2] = new SqlParameter("@FirstName", FirstName);
            pars[3] = new SqlParameter("@LastName", LastName);
            pars[4] = new SqlParameter("@BirthDay", BirthDay);
            pars[5] = new SqlParameter("@TypeID", TypeID);
            pars[6] = new SqlParameter("@Address", Address);
            pars[7] = new SqlParameter("@Description", Description);

            db.ExecuteNonQuerySP("sp_Family_Insert", pars);
            FamilyID = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Cập nhật Family
        /// </summary>
        public void Update()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[9];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@FamilyID", FamilyID);
            pars[2] = new SqlParameter("@EmployeeID", EmployeeID);
            pars[3] = new SqlParameter("@FirstName", FirstName);
            pars[4] = new SqlParameter("@LastName", LastName);
            pars[5] = new SqlParameter("@BirthDay", BirthDay);
            pars[6] = new SqlParameter("@TypeID", TypeID);
            pars[7] = new SqlParameter("@Address", Address);
            pars[8] = new SqlParameter("@Description", Description);

            db.ExecuteNonQuerySP("sp_Family_Update", pars);
            ReturnValue = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Xoá Family
        /// </summary>
        /// <param name="familyID"></param>
        public void Delete(int familyID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            
            db.ExecuteNonQuerySP("sp_Family_Delete",
                new SqlParameter("@FamilyID", familyID));
        }

        /// <summary>
        /// Xoá Contract
        /// </summary>
        public void Delete()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);

            db.ExecuteNonQuerySP("sp_Family_Delete",
                new SqlParameter("@FamilyID", FamilyID));
        }

        public DataTable GetTList(int employeeID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetDataTableSP("sp_Family_SelectList", new SqlParameter("@EmployeeID", employeeID));
        }

        public List<Family> GetList()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetListSP<Family>("sp_Family_SelectList");
        }

    }
}
